<%@ page import="com.work.bean.ClassW" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/10/7
  Time: 14:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>我的班级</title>
</head>
<%@include file="teacher_common.jsp" %>
<script>
    $(function () {
        $("#my_class_li").attr('class', 'active');
    });
</script>

<style>
    th, td {
        width: 160px;
        padding: 10px;
        background-color: rgb(180, 193, 224);
    }
</style>

<%
    List<ClassW> classes = (List<ClassW>) session.getAttribute("classes");

    int pageNumber = (Integer) session.getAttribute("pageNumber");
    int whichPage = (Integer) session.getAttribute("whichPage");
    int countPage = (Integer) session.getAttribute("countPage");
    boolean flag = false;
    if (session.getAttribute("flag") != null) {
        flag = (Boolean) session.getAttribute("flag");
    }
//
//    String menuCategory = "";
//
//    if (session.getAttribute("typeName") != null) {
//        menuCategory = (String) session.getAttribute("typeName");
//    }

    if (session.getAttribute("error") != null) {
        out.print(session.getAttribute("error"));
    }

    boolean isDis = false;

    if (session.getAttribute("workID") != null) {
        isDis = true;
    }
%>


<body>

<div>
    <div>
        <div style="margin-left: 250px">
            <h3 style="margin-top: 50px">班级列表</h3>
            <%
                if (isDis) {%>
            <form action="${pageContext.request.contextPath}/distribute_work" method="post">
                <input type="hidden" name="workId"
                       value="<%=session.getAttribute("workID") != null?session.getAttribute("workID"):0%>">
                <%
                    }
                %>
                <center>
                <table border="1px" cellspacing="0px" cellpadding="20px" style="margin-left: 10px">
                    <thead>
                    <tr>

                        <%

                            if (isDis) {%>

                        <th></th>
                        <%
                            }
                        %>

                        <th>班级名称</th>
                        <%

                            if (!isDis) {%>
                        <th><a href="teacher_add_class.jsp">添加</a></th>
                        <%
                            }
                        %>
                    </tr>
                    </thead>
                    <tbody>
                    <%

                        if (classes != null) {
                            for (ClassW classW : classes) {

                    %>

                    <tr>

                        <%

                            if (isDis) {%>

                        <td><input type="checkbox" name="checkbox" checked="checked" value="<%=classW.getId()%>"></td>
                        <%
                            }
                        %>
                        <td><a href="${pageContext.request.contextPath}/my_student"><%=classW.getClassName()%>
                        </a>
                        </td>

                        <%

                            if (!isDis) {%>
                        <td>
                            <a href="${pageContext.request.contextPath}/teacher_add_class.jsp?id=<%=classW.getId()%>&editName=<%=classW.getClassName()%>">修改</a>
                            | <a href="#" onclick="deleteById(<%=classW.getId()%>)">删除</a></td>
                        <%
                            }
                        %>

                    </tr>
                    <%
                        if (isDis) {%>
                    <tr>
                        <td colspan="2"><input type="submit" value="确认分配"></td>
                    </tr>
                    <%}%>

                    <%
                        }
                    } else {
                    %>

                    <tr>
                        <td colspan="4">暂无班级，可点击添加进行创建班级</td>
                    </tr>
                    <%
                        }
                    %>

                    <tr>
                        <td colspan="5" align="center">
                            <button onclick="jump('first')">最前页</button>
                            <button onclick="jump('last')">上一页</button>
                            <button onclick="jump('next')">下一页</button>
                            <button onclick="jump('final')">最后页</button>
                            跳转到第<select id="whichPage" onchange="jumpChange(0,this[selectedIndex].value)">
                            <%
                                if (pageNumber != 0) {

                                    for (int i = 0; i < pageNumber; i++) {

                                        if ((i + 1) == whichPage) {
                            %>
                            <option selected="selected"><%=i + 1%>
                            </option>
                            <%
                            } else {%>
                            <option><%=i + 1%>
                            </option>
                            <%
                                        }
                                    }

                                }
                            %>

                        </select>页
                            每页显示
                            <select id="pageCount" onchange="jumpChange(1,this[selectedIndex].value)">

                                <option><%=countPage%>
                                </option>
                                <%

                                    for (int i = 3; i <= 15; i += 3) {
                                        if (i == countPage) {%>

                                <option selected="selected"><%=countPage%>
                                </option>
                                <%} else {%>
                                <option><%=i%>
                                </option>
                                <%
                                        }

                                    }
                                %>
                            </select>条

                        </td>
                    </tr>

                    </tbody>
                </table>
                </center>

            </form>
        </div>

        <!--//banner-->
    </div>
</div>

</body>

<script type="text/javascript">

    function deleteById(id) {

        if (confirm("确认删除？")) {
            window.location = "/my_class?deleteId=" + id;
            alert("删除成功");
        }
    }

    function jump(type) {

        var objW = document.getElementById("whichPage"); //定位id
        var indexW = objW.selectedIndex; // 选中索引
        var whichPage = objW.options[indexW].text; // 选中文本

        var objP = document.getElementById("pageCount"); //定位id
        var indexP = objP.selectedIndex; // 选中索引
        var pageCount = objP.options[indexP].text; // 选中文本

        if (type == 'first') {

            if (whichPage == 1) {

                alert("当前已是第一页");

            } else {

                window.location = "/my_class?countPage=" + pageCount + "&whichPage=1";
            }

        } else if (type == 'last') {

            if (whichPage == 1) {
                alert("当前已是第一页");
            } else {

                window.location = "/my_class?countPage=" + pageCount + "&whichPage=" + (whichPage - 1);
            }

        } else if (type == 'next') {

            if (whichPage == <%=pageNumber%>) {

                alert("当前已是最后页");

            } else {

                window.location = "/my_class?countPage=" + pageCount + "&whichPage=" + (whichPage - (-1));
            }

        } else if (type == 'final') {

            if (whichPage == <%=pageNumber%>) {
                alert("当前已是最后页");
            } else {

                window.location = "/my_class?countPage=" + pageCount + "&whichPage=<%=pageNumber%>";
            }
        }

    }

    function jumpChange(type, value) {

        if (type == 0) {

            var objP = document.getElementById("pageCount"); //定位id
            var indexP = objP.selectedIndex; // 选中索引
            var pageCount = objP.options[indexP].text; // 选中文本

            window.location = "/my_class?countPage=" + pageCount + "&whichPage=" + value;

        } else if (type == 1) {

            var objW = document.getElementById("whichPage"); //定位id
            var indexW = objW.selectedIndex; // 选中索引
            var whichPage = objW.options[indexW].text; // 选中文本

            window.location = "/my_class?countPage=" + value + "&whichPage=1";

        }
    }

</script>
</html>
